How to calculate machine total hours and working hours using the tag history?

You can use the use Timestamp property of the machine_working_hour to calculate the time delta between the last state change and current time, and have this time added to a accumulation tag (memory tag that stores seconds or minutes ).

You can use a tag onChange script perform the calculation and storage of the time elapsed to the accumulation tag.