New Project: Home Automation

The topic “Home Automation” keep popping into my head for years now. I always questioned if it would be worth the investment. Both, financially and from a time effort perspective.

But I decided to give it a try. Mainly because it is the safest way to judge if it is worth it, once and for all. And there should be plenty possibilities for learning and trying out new things on the way.

So my first “goal” so to say is:

  • Home Automation software running
  • Some sensors for temperature/humidity
  • A dashboard for all rooms

Once I achieved that, I will check how to go on…

Default parameters in bash

Often you want to use default values in a bash script. Mostly when you want to make use of optional arguments. You can easily achieve that with parameter substitution.

unset VAR
VAR=${VAR-default}
echo $VAR        # VAR is "default"

VAR=${VAR-another default}
echo $VAR        # VAR is still "default"

There are more parameter substitutions which you can checkout at http://tldp.org/LDP/abs/html/parameter-substitution.html

How to access a specific array element using jq?

Let’s assume we have following JSON document

$cat > data.json <<EOF
→ { "data" : [
→   { "key": "a", "value": 1 },
→   { "key": "b", "value": 2 }
→ ]}
→ EOF

Now, we want to access the value where the key is “b”. In this specific case, we can of course do so with

$ cat data.json  | jq '.data[1].value'
2

But this relies on the order in the array and the data itself. A more generic and stable way would be

$ cat data.json  | jq '.data[]  | select(.key == "b") | .value'
2

For more details on jq, you can read https://shapeshed.com/jq-json/.

How to access the name of a pod?

Sometimes you need the name of a pod within a script (e.g. with kubectl exec). But since you normally want to use a deployment or replica set to manage your pod, the name of the pod might change and cannot be predicted.

There is an easy way to access the name of the pod via ‘kubectl’ with the help of labels. In this example we access the name of the pod described by the label label=value.

$kubectl get pod -l label=value -o jsonpath='{.items[0].metadata.name}'

For details on the jsonpath parameter can be found at https://kubernetes.io/docs/reference/kubectl/jsonpath/.

How to create an example kubernetes deployment?

Here is a simple example of how to create a kubernetes deployment out of a yaml file.

cat << EOF > client.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: client
  labels:
    app: client
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
      - name: client
        image: ubuntu:18.04
        command: ["sleep"]
        args: ["3600"]
      restartPolicy: Always
EOF

The file can then be applied with kubectl apply -f client.yaml.

To verify the pod being started, just run kubectl get pod -l app=client.

Accessing a port on a pod

With kubectl, you can just forward a specific port of some pod to your local machine. So you can test the pod. A simple example:

# Start a grafana pod
kubectl run grafana --image=grafana/grafana
POD=$(kubectl get pod -l run= grafana \ 
    -o jsonpath="{.items[0].metadata.name}")

# Forward port 3000
kubectl port-forward $POD 3000:3000 &

# Access port
curl -s http://localhost:3000/login | grep title