Can a sensor's state record the previous state of another sensor?

I have a sensor that keeps track of when the next spring or neaps tide will be. I’d like also to retain the previous spring or neaps tide. My plan was to use a trigger sensor for the last_tide_state that was triggered when the next_tide_state changes, but how do I set last_tide_state to the previous value when next_tide_state now contains its new state?
The rest sensor that sets next_tide_state gets a new 6 days set of tides once a day and the neaps/springs tide changes state once a week (or so).

Hi @DJI

Assuming that your trigger is a state trigger then you should have the variable trigger.from_state available.

If this doesn’t help then post the code for your sensor, so we can have a look.

Check the previous post :slight_smile:

Check trigger.from_state.state to find out previous one.

Wow, thanks for the fast replies!
I’ve never written a state trigger before & the example given looks like an automation. Is it an automation that I need then?

Would I be right in thinking that I add something like this to automations.yaml?

automation:
  trigger:
    - platform: state
      entity_id: sensor.next_tide_state
      name: last_tide_state
      value: '{{trigger.from.state}}'

or am I one the wrong track?

Try this (configuration.yaml):

template:
  - trigger:
      - platform: state
        entity_id: sensor.next_tide_state
        to: # null 'to' triggers on any state change but ignores attribute changes.
    sensor:
      - name: Last Tide State
        icon: mdi:waves
        state: "{{ trigger.from_state.state }}"

Thanks Tom, I’ll try that on tonight’s run. Should the last line read state: "{{ states('trigger.from.state') }}" because I read somewhere you should use state( and avoid entity.state?

No. The states() method is how you access the state value of an object. trigger.from_state is a type of variable that contains a state object. See: Automation Trigger Variables - Home Assistant

Confused?

I am :upside_down_face: but the way I wrote it is correct.

It’s all too clever for me, but I’m gradually getting the hang of some things by trial, error and the invaluable help of all you good folk out there!

1 Like

Don’t stress, there’s a lot to learn. Small steps and you’ll get there eventually.

Well that didn’t work! I can’t see what I got wrong. The trouble with a sensor that only changes value every week or so is how to test it. It changed last night but the old value wasn’t captured. My trigger sensor looks like this (apologies for the large pcture but I couldnt see how to show that it did change value last night):

  - trigger:
      - platform: state
        entity_id: sensor.uktiderange
        to: # null 'to' triggers on any state change but ignores attribute changes.
    sensor:
      - name: lastuktiderangestate
        icon: mdi:waves
        state: "{{ trigger.from_state.state }}"
        attributes:
          event: "{{ trigger.from_state.state.split()[-2] }}"
          when: "{{ trigger.from_state.state.split('/')[1] }}"
          whentxt: >
            {% set whn = as_timestamp(as_datetime(trigger.from_state.state.split('/')[1])) %}
            {% set intvl = ((whn - as_timestamp(as_datetime(states('sensor.tide_next'))))/(24*3600))|abs %}
            {% if intvl < 1 %}
              {{''}}
            {% else %}
              {{+ intvl|int ~ " days ago" }}
            {% endif %}  

The values are:

lastuktiderangestate unavailable 
icon: mdi:waves 
friendly_name: lastuktiderangestate


UKtiderange 
friendly_name: UKtiderange

On further investigation I can see from the logbook that the source sensor UKtiderange becomes unavailable for less than a second before it is evaluated every morning (when a new set of tides is obtianed by the REST sensor). How do I get my set-up to ignore this (or prevent it from happening)?

Is this how you do it? I won’t know until it next triggers unfortunately.

  - trigger:
      - platform: state
        entity_id: sensor.uktiderange
        not_to: 
          - unkown
          - unavailable
    sensor:
      - name: lastuktiderangestate
        icon: mdi:waves
        state: "{{ trigger.from_state.state }}"
        attributes:
          event: "{{ trigger.from_state.state.split()[-2] }}"
          when: "{{ trigger.from_state.state.split('/')[1] }}"
          whentxt: >
            {% set whn = as_timestamp(as_datetime(trigger.from_state.state.split('/')[1])) %}
            {% set intvl = ((whn - as_timestamp(as_datetime(states('sensor.tide_next'))))/(24*3600))|abs %}
            {% if intvl < 1 %}
              {{''}}
            {% else %}
              {{+ intvl|int ~ " days ago" }}
            {% endif %}