Copy switch state (i.e. use magnetic sensor)

Hi!

I’m trying to get my 433 MHz magnetic sensor to change the state for one of my switches (i.e. when I open a door the switch will turn on). I’m using a Tellstick Duo and I’ve managed to make the system register when I open/close the sensor (now it is actually a switch and not a sensor).

I tried to use the “Copy switch” example from https://home-assistant.io/components/switch.template/ but I have problem understanding the " value_template: ‘{{ states.switch.source.state }}’" part of it.

switch:
    platform: template
    switches:
        copy:
            value_template: '{{ states.magnetic.switch.switch_31.state }}'
            turn_on:
                service: switch.turn_on
                entity_id: switch.magnetic
            turn_off:
                service: switch.turn_on
                entity_id: switch.switch_31

So what I’m trying to do is to get switch.switch_31 to turn on when switch.magnetic goes from the state off to the state on. Am I doing it the wrong way? How does the "value_template: "-part work?

Also, i get this error message in the log: “homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false”
//F

Try this:

switch:
  platform: template
  switches:
    copy:
      value_template: '{{ states.switch.switch_magnetic.state }}'
      turn_on:
        service: switch.turn_on
        entity_id: switch.switch_31
      turn_off:
        service: switch.turn_off
        entity_id: switch.switch_31

The value template will show the state of the magnetic switch. When that is on, the turn on service will be called, and when the magnetic switch is off, the turn off service will be called.

Hmmm,

I still get “16-07-08 19:49:50 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false”

I copied this into my configuration.yaml

switch:
    platform: template
    switches:
        copy:
            value_template: '{{ states.switch.switch_magnetic.state }}'
            turn_on:
                service: switch.turn_on
                entity_id: switch.switch_31
            turn_off:
                service: switch.turn_off
                entity_id: switch.switch_31

The switches look like this in HA:

The entity id for the magnetic switch is wrong.

switch:
  platform: template
  switches:
    copy:
      value_template: '{{ states.switch.magnetic.state }}'
      turn_on:
        service: switch.turn_on
        entity_id: switch.switch_31
      turn_off:
        service: switch.turn_off
        entity_id: switch.switch_31

Thanks for the help Martin! However, for some reason it seems like the
value_template: ‘{{ states.switch.magnetic.state }}’ part is not working.

I still get:

16-07-09 09:13:43 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:43 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:43 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:43 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false
16-07-09 09:13:44 homeassistant.components.switch.template: Received invalid switch is_on state: . Expected: on, off, true, false

Any other ideas? I’ve tried using other switches, but the state command does not return the expected state for some reason…

You could try double quotes instead of single quotes around the template.

I tried the double quotes, but still same error. I can get around it by pairing the magnetic sensor to my switch outside HA to get the functionality. However, the state of the switch won’t update (the magnetic sensor state updates at is suppose to) in HA so it feels like a less elegant way of solving the issue. And it is still strange that the state-thing won’t work. Is there any other way of checking what the value_template: ‘{{ states.switch.magnetic.state }}’ is returning now?

There’s a template editor among the developer tools in the HA GUI.

When I put my code into the template editor it gives me

switch:  
  platform: template
  switches: 
    copy: 
      value_template: 'off'
      turn_on: 
        service: switch.turn_on
        entity_id: switch.switch_31
      turn_off: 
        service: switch.turn_off
        entity_id: switch.switch_31

So the value_template apparently returns ‘off’. I tried removing the ’ ’ around {{ states.switch.magnetic.state }} , but this prevents HA from starting at all and I don’t get any error messages in the HA-logfile (restarting my rPi did not help either).

Everything looks good to me, except that it’s not working. :disappointed:

I’ll have a look at the source and see if I can find something to try.

Edit: Have you tried to switch the magnetic sensor after HA start and see if the other switch responds? I’m thinking the error in the log might come during upstart of HA when the magnetic sensor isn’t setup yet.

Very strange :slight_smile:

I have tried to switch the magnetic sensor after HA is up and running, but nothing happens to switch_31 (the magnetic switch behaves as I expect it too). I did try to see if there was any change if I used other switches to follow each other, but the only difference I got was the number of error messages in the log (one switch only gave 4 error messages, while the magnetic one returns 14 of them. So it seems like depending on which order HA loads the switches I get different number of errors :slight_smile: )

You can add a key entity_id to the config with the entity_id of the magnetic sensor. That way the switch will only try to update state when the state of the magnetic sensor changes. If not a solution, it should narrow down the possible problems and filter the log errors.

switch:
  platform: template
  switches:
    copy:
      value_template: '{{ states.switch.magnetic.state }}'
      turn_on:
        service: switch.turn_on
        entity_id: switch.switch_31
      turn_off:
        service: switch.turn_off
        entity_id: switch.switch_31
      entity_id: switch.magnetic

Btw, what kind of platform are you using for the magnetic switch/sensor?

So adding the “entity_id: switch.magnetic” line reduced the number of error messages to 1.

I’m using a Raspberry Pi 2 (Lates version of Raspbian Jessie & HA 0.23.1) with a Tellstick Duo (latest firmeware). The magnetic sensor is a Nexa LMST-606 and the switch I’m trying to control is a Proove TSP200.

1 Like

Try changing the value template to:

value_template: "{% if is_state('switch.magnetic', 'on') %}on{% else %}off{% endif %}"

You should compare the result of that with:

value_template: "{{ states.switch.magnetic.state }}"

By using "value_template: “{% if is_state(‘switch.magnetic’, ‘on’) %}on{% else %}off{% endif %}” the error messages are gone :slight_smile:

But no improvement on the switch-thing. The state still does not update on the switch I want to follow… Would be interesting to hear if someone has managed to get this working…

You need to use an automation to accomplish what you’re trying to do.

With two automations:

automation:
  - trigger:
      platform: state
      entity_id: switch.magnetic
      to: 'on'
    action:
      service: switch.turn_on
      entity_id: switch.switch_31
  
  - trigger:
      platform: state
      entity_id: switch.magnetic
      to: 'off'
    action:
      service: switch.turn_off
      entity_id: switch.switch_31

It might also be possible with just one automation (untested):

automation:
  - trigger:
      platform: state
      entity_id: switch.magnetic
    action:
      service_template: "switch.turn_{{ states('switch.magnetic') }}"
      entity_id: switch.switch_31

If the one-automation version produces an error, try using this instead:

automation:
  - trigger:
      platform: state
      entity_id: switch.magnetic
    action:
      service_template: >
        {%- if states('switch.magnetic') == 'on' -%}
          switch.turn_on
        {%- else -%}
          switch.turn_off
        {%- endif -%}
      entity_id: switch.switch_31
1 Like

Thanks dale3h! Worked perfectly with the automation code!

Hello again, still having some issues here it seems…

But I think I know why now! So I did a test where I had another automation changing the state of switch B when the state of switch A changed. So magnetic breaker changes state of switch A and switch A changes state of switch B. The state is updated for all of them in HASS, but the Tellstick signal to switch A does not reach the switch (but switch B gets the signal). However, when I use the button in HASS to change the state of the magnetic switch both signals reach switch A &B. So it seems like there is a timing issue.

The solution, I think, would be to add a short delay (because it seems like HASS has that when you press the button) before the action starts. Is it possible to do this in a simple way?

//F

I’m not following your conclusion to your problem. But to answer your question, yes, you can use a script to add a delay before or in between actions.

Ahh, perhaps I was a bit unclear… I have been looking into the script component, but I lack the skills to understand exactly what to do…

I’m using this code at the moment:

  - trigger:
      platform: state
      entity_id: switch.magnetic
        
     action:
                    
       service_template: >
         {%- if states('switch.magnetic') == 'on' -%}
           switch.turn_on
         {%- else -%}
           switch.turn_off
         {%- endif -%}
       entity_id: switch.switch_31_2

And this works when I use the toggle in HASS, but when I use the physical magnetic breaker the state in HASS changes but it seems like the signal to the switch is not being sent out (from the Tellstick duo). Therefore I would like to test to delay the action with, e.g. 2 seconds, to see if this makes any difference.

I tried to modify the action code (I looked at this https://home-assistant.io/getting-started/automation-action/ ) , but this was over my head…Any advice or example codes I can look at?

Thanks!