Calculation inside automation

hi, I need some help how can I do some simple calculations inside an automation?

(x * 2) + y = z

X = {% if is_state(‘binary_sensor.power1_sensor’, ‘on’) %}
‘{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.power1_sensor.last_changed))}}’

Y = {{ (states.cover.balcony_bedroom_cover.attributes.current_position) }}

Then by using the resulting Z value to update the service cover.set_cover_position

can you please help ?!!!

Are you familiar with the templates tab on the front end? Try creating an expression for “z” in there, and it should allow you to quickly iterate and figure out the exact syntax.

Hi thank you for trying to help, I am not familiar with the template feature, only I have used this in order to test that the templates I am using here return the correct values, but I cannot find a way to figure out the correct syntax in order to do those calculations.

I am new to home assistant and I started using templates just a few days ago. I think for some of you experienced in templating would not be such a great deal to provide an example for me.

Thank you.

{% set x = is_state(‘binary_sensor.power1_sensor’, ‘on’) %}
{% set y = states.cover.balcony_bedroom_cover.attributes.current_position %}
{{ (x * 2) + y }}

This equation doesnt make sense though because you are setting x to a state. But thats how you set values to objects.

You should look up how to use JINJA. That’s the coding language that is supported in the templates.

thank you petro , X after calculation is always a number. X i want to be calculated by this

{% if is_state('binary_sensor.power1_sensor', 'on') %}
{{(as_timestamp(now()) - as_timestamp(states.binary_sensor.power1_sensor.last_changed))}}
{% else %}
0
{% endif %}

this results in a number as it counts the time the power is on to one of the two switches in a sonoff dual.
by knowing the time I can calculate the position the shutter has been moved. so how can i set X as the result of the above template ? thank you

template sensor