State of one entity depending on other 2 entities

Hi All,

First of all, thank you to this great community. I have HA for 2 years and it is the first time I need to write a post (because all the information is there!)

My set up:

  1. A synology running a docker container with HA and mainly tasmotized sonoffs
  2. I have an electric valve that controls whether I use the hot water coming from the solar energy system or from the water heater. I control that with 2 relays of a sonoff 4CH Pro with tasmota
  3. I have an automation that every time the temperature of the water in the solar system goes below 35 degrees it switches the relay to move the valve and use the boiler (and switches it off after 30 seconds) and if it goes above 35 it switches the other relay to move the valve in the other direction.

My 2 problems

  1. I want to know when I am using the solar or the electric system in a given moment. I thought that I could create a new entity that would capture when each of the relays goes to ON but I do not know if this is possible (or even if it is a good idea)

  2. The automation is triggered every time the sensor updates the temperature. How can I trigger the automation just when the temperature crosses 35 degrees in one or other direction?

Any idea is welcomed. Thank you in advance!

Daniel

For 1: Use a template sensor

For 2: In your automation trigger on sensor state change then use a condition section to test for your set temperature along with trigger.to_state and trigger.from_state to determine direction. The from_state is the previous trigger state and the to_state is the current state. So depending on which is larger you have direction.

Hi micque,

Thanks for pointing me in the right direction. It took a while for me to learn about template sensors (first time I use it). It is not working so far. I created a binary sensor that captures when the other 2 entities are ON. It is not working so far. But actually, I want to capture the change to On in each of the entities and keep it like this until the other entity is On. Here is my code so far:

- platform: template
    sensors:
      placa_termo:
        entity_id:
          - switch.placa
          - switch.termo
        value_template: >-
          {% if is_state('switch.placa','on') %}
            Placa
          {% elif is_state('switch.termo', 'on') %}  
            Termo  
          {% else %}
            A subir
          {% endif %}
        icon_template: >-
          {% if is_state("binary_sensor.placa_termo", "Placa") %}
            mdi:solar-power
          {% elif is_state("binary_sensor.placa_termo", "Termo") %}
            mdi:radiator
          {% else %}
            mdi:help-network
          {% endif %}

Any idea what is wrong?

Daniel

If the example you posted is supposed to be a Template Binary Sensor, then the value_template is incorrect. The template should return on or off but it’s reporting Placa, Termo, and A subir.

If you need to show multiple states like that, it should be a Template Sensor (not a Template Binary Sensor).

Hi Taras,

Thanks for the answer. You were right and if works now.

Now my problem is that when switch.placa goes off then the state of the sensor changes to “A subir”. I want to keek the state until switch.termo goes ON. In a table it would be:

Time Switch State Sensor
1 switch.placa ON Placa
2 switch.placa OFF Placa
3 switch.placa OFF Placa
.
.
.
x switch.termo ON Termo
x+1 switch.termo OFF Termo
.
.
y switch.placa ON Placa
and so on.

I hope it is clear.

Cheers,
Daniel

Unfortunately, no, I do not understand how you want it to work.

You have two switches and each switch has two states. That means there are 22=4 possible combinations. Please let me know the four sensor states you want in the following table:

value switch.placa switch.termo sensor
0 off off ?
1 off on ?
2 on off ?
3 on on ?

Hi Taras,

Want I want to capture is the last switch that turned ON.

It does not matter the current state or if they turn OFF. I have an automation that turns OFF 30 seconds after they are turned ON so most of the day they will be OFF. So I want to have a sensor to know which one was turned on last (that will determine whether I am using water coming from the Solar or boiler system).

I hope is clearer now.

Daniel

OK. If you are currently using MQTT, this is how I would do it:

Create an MQTT Sensor:

  - platform: mqtt
    name: 'Placa Termo'
    state_topic: 'sensor/placa_termo'

Create an automation:

  - alias: 'Placa Termo'
    trigger:
      platform: state
      entity_id:
        - switch.placa
        - switch.termo
      to: 'on'
    action:
      service: mqtt.publish
      data_template:
        topic: sensor/placa_termo
        payload: '{{ trigger.to_state.object_id }}'
        retain: true

When either switch changes state to on its name (object_id) is published to the topic sensor/placa_termo. The sensor is subscribed to this topic and so its state will indicate either placa or termo, whichever was the last one to turn on.

NOTE:
The sensor’s initial state will be unknown and will only change when one of the two switches turns on.

The switch’s name is published as a retained message. This means the MQTT Broker will store the payload. When Home Assistant restarts, the sensor will re-subscribe to sensor/placa_termo and get last stored message (either placa or termo).

Hi Taras,

It works fantastic! Thank you so much. May I use your brain for the second problem?

I want switch.placa and switch.termo to be triggered when the temperature goes above or below 35 degrees respectively. The problem is that the automation keep triggering every time the temperature sensor updates the temperature (and it is different to the previous one). This is the code of the automations that I have:

  - alias: Agua del termo
    trigger:
    - platform: numeric_state
      entity_id: sensor.temperatura_placa
      below: 34
    condition:
    - condition: time
      after: '07:00:00'
      before: '21:00:00'
    action:
    - service: switch.turn_on
      entity_id: switch.termo
    - delay: 00:00:30
    - service: switch.turn_off
      entity_id: switch.termo
  
  - alias: Agua de placa
    trigger:
    - platform: numeric_state
      entity_id: sensor.temperatura_placa
      above: 35
    action:
    - service: switch.turn_on
      entity_id: switch.placa
    - delay: 00:00:30
    - service: switch.turn_off
      entity_id: switch.placa

I just want the automation to be triggered when the temperature crosses above or below 35 but not if they stay all the time above or below.

Cheers,
Daniel

Are you certain of that?

A Numeric State Trigger only triggers when the entity’s value crosses the threshold value. In your first automation, it will trigger when temperatura_placa crosses the threshold of 34. In other words, when it is equal to or less than 34 and only the first time it happens. After it crosses the threshold, any further decrease does not re-trigger the automation. The value of temperatura_placa has to first rise above 34, then fall below it to re-trigger the automation. That’s how a Numeric State Trigger works.

Hi Taras,

You were right (again). It took me a while to figure the issue.

It only triggers when it crosses the threshold. The issue is that it is hot water and when we do not use it is reduces the temperature progressively until it crosses again the threshold. When we open it again the hot water reaches the temperature sensor and triggers the automation. Not sure how to fix this but it is not a big deal.

Thanks!

Cheers,
Daniel

You’re welcome! I’m glad to hear it works well.

Please mark my post (containing the MQTT Sensor example) with the Solution tag. Only you, the author of this topic, can do that. It will automatically place a check-mark next to the topic’s title, signaling to others that this topic has an accepted solution. It will also automatically place a link beneath your first post that leads to the solution. All of this helps others who may have a similar question and are looking for answers.